﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace guest_management_in_a_hotel
{
    public partial class FrmThongTinKH : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader rd;
        public static int socmnd = Int32.Parse(FrmTimKiemKhach.CMND);
        public FrmThongTinKH()
        {
            InitializeComponent();
        }

        private void FrmThongTinKH_Load(object sender, EventArgs e)
        {
            
            conn = Connect.getConnection();
            try
            {
                conn.Open();
                String strsql = "select Guest_Passport_No,Guest_Name,Guest_Sex,Guest_DOB,Guest_Address,Guest_Nationality from TblGuests where Guest_Passport_No='" + socmnd + "'";
                cmd = new SqlCommand(strsql, conn);
                rd = cmd.ExecuteReader();
                if (rd.Read())
                {
                    tbcmnd.Text = rd.GetInt32(0).ToString();
                    tbhoten.Text = rd.GetString(1);
                    tbgioitinh.Text = rd.GetString(2);
                    tbngaysinh.Text = rd.GetDateTime(3).ToString();
                    tbdiachia.Text = rd.GetString(4);
                    tbquoctich.Text = rd.GetString(5);

                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn = Connect.getConnection();
            try
            {
                if (conn != null)
                {
                    conn.Open();
                    String strsql = "UPDATE TblGuests SET Guest_Passport_No='" + Int32.Parse(tbcmnd.Text) + "',Guest_Name=N'" + tbhoten.Text + "',Guest_Sex=N'" + tbgioitinh.Text + "',Guest_DOB='" + DateTime.Parse(tbngaysinh.Text).ToShortDateString() +"',Guest_Address=N'" + tbdiachia.Text + "',Guest_Nationality=N'" + tbquoctich.Text + "' where Guest_Passport_No='" +socmnd+ "'";
                    
                    cmd = new SqlCommand(strsql, conn);
                    rd = cmd.ExecuteReader();
                    conn.Close();
                    MessageBox.Show("Cập nhật thành công", "Sửa thông tin khách hàng");
                    FrmTimKiemKhach frmtkk = new FrmTimKiemKhach();
                    frmtkk.Show();
                    this.Hide();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi sửa thông tin khách hàng", "Mời nhập lại");

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FrmTimKiemKhach frmtkkh = new FrmTimKiemKhach();
            frmtkkh.Show();
            this.Hide();
        }

    }
}
